#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define max(a,b) (((a) > (b)) ? (a) : (b))

char s0[32], s1[128][32], s3[32], out[128][32], *p, blist[128][128] = { 0 }, *z, *x, *y;
char oList[128], o;
int fCount, eCount, outIndex = 0, oIndex, count;

int toInt(char *c)
{
    int ret = 0;
    while (*c)
    {
        ret *= 10;
        ret += *c - ('0' - 0);
        c++;
    }
    return ret;
}

//e: ERR, 0: 不执行, 1: 线性, n: n
char getO(char *i, char *x, char *y)
{
    int ix, iy;

    if (!strcmp(x, "n"))
    {
        if (!strcmp(y, "n"))
        {
            return '1';
        }
        
        return '0';
    }
    
    if (!strcmp(y, "n"))
    {
        return 'n';
    }
    
    // printf("-----%s %s", x, y);
    ix = toInt(x);
    iy = toInt(y); 
    // printf("-----%d %d", ix, iy);
    if (ix > iy)
    {
        return '0';
    }
    else
    {
        return '1';
    }
}

int main(int argc, char const *argv[])
{
    int t, l;
    int i, j, k, r, lr, bListCount = 0, isInList;
    scanf("%d", &t);

    for (i = 0; i < t; i++)
    {
        //代码行数
        scanf("%d ", &l);
        //输入给的答案
        gets(s0); 
        
        //读取所有代码
        for (j = 0; j < l; j++)
        {
            gets(s1[j]);
        }
        
        //该段代码最终答案
        r = 0;
        
        fCount = 0;
        eCount = 0;
        oIndex = 0;
        bListCount = 0;
        for (j = 0; j < l; j++)
        {
            //如果是for则压入
            if (s1[j][0] == 'F')
            {
                if (eCount != 0)
                {
                    lr = 0;

                    for (k = fCount; k > eCount; k--)
                    {
                        if (oList[k] == '0')
                        {
                            break;
                        }
                        if (oList[k] == '1')
                        {
                            continue;
                        }
                        if (oList[k] == 'n')
                        {
                            lr++;
                        }
                    }

                    r = max(r, lr);
                    bListCount -= eCount;
                    fCount -= eCount;
                    oIndex -= eCount;
                    eCount = 0;
                }
                
                strtok(s1[j], " ");
                z = strtok(NULL, " ");
                x = strtok(NULL, " ");
                y = strtok(NULL, " ");

                isInList = 0;
                for (k = 0; k < bListCount; k++)
                {
                    if (!strcmp(z, blist[k]))
                    {
                        isInList = 1;
                        break;
                    }
                }

                if (isInList)
                {
                    r = -1;
                    break;
                }

                strcpy(blist[bListCount++], z);

                o = getO(z, x, y);

                if (o == 'e')
                {
                    r = -1;
                    break;
                }

                oList[oIndex] = o;    
                oIndex++;
                fCount++;
            }
            else if (s1[j][0] == 'E')
            {
                if (fCount == 0)
                {
                    r = -1;
                    break;
                }

                eCount++;
                if (eCount == fCount)
                {
                    lr = 0;

                    for (k = 0; k < oIndex; k++)
                    {
                        if (oList[k] == '0')
                        {
                            break;
                        }
                        if (oList[k] == '1')
                        {
                            continue;
                        }
                        if (oList[k] == 'n')
                        {
                            lr++;
                        }
                    }
                    // printf("lr---%d\n", lr);

                    r = max(r, lr);
                    bListCount = 0;
                    eCount = 0;
                    fCount = 0;
                    oIndex = 0;
                }
            }
        }

        if (r == -1 || eCount != 0 || fCount != 0)
        {
            strcpy(out[outIndex++], "ERR");
        }
        else if (r == 0)
        {
            strcpy(out[outIndex++], (!strcmp("O(1)", s0) ? "Yes" : "No"));
        }
        else
        {
            // printf("---%d", r);
            sprintf(s3, "O(n^%d)", r);
            strcpy(out[outIndex++], (!strcmp(s3, s0) ? "Yes" : "No"));
        }
    }
    for (i = 0; i < outIndex; i++)
    {
        if (!i)
        {
            printf("%s", out[i]);
        }
        else
        {
            printf("\n%s", out[i]);
        }
    }
    
    return 0;
}